package com.gopay.common.manager.ptbank.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.constants.ptbank.HfSuperviseCode;
import com.gopay.common.constants.ptbank.HfSuperviseCode.BANKSTATE;
import com.gopay.common.dao.ptbank.GsrtBankSuperviseLogDao;
import com.gopay.common.domain.gsrt.GsrtCustTenderMaster;
import com.gopay.common.domain.ptbank.GsrtBankCustSuperviseInfo;
import com.gopay.common.domain.ptbank.GsrtBankServiceInfo;
import com.gopay.common.domain.ptbank.GsrtBankSuperviseLog;
import com.gopay.common.domain.ptbank.invo.HfCommonHead;
import com.gopay.common.manager.ptbank.GsrtBankCustSuperviseInfoManager;
import com.gopay.common.manager.ptbank.GsrtBankServiceInfoManager;
import com.gopay.common.manager.ptbank.GsrtBankSuperviseLogManager;
import com.gopay.common.util.DateUtils;

/**
 * 银行托管日志表
 * GsrtBankSuperviseLogManagerImpl.java
 * 2016年4月27日
 * @author donghuijun
 *  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("gsrtBankSuperviseLogManager")
public class GsrtBankSuperviseLogManagerImpl implements GsrtBankSuperviseLogManager{
	@Resource(name = "gsrtBankSuperviseLogDao")
    private GsrtBankSuperviseLogDao gsrtBankSuperviseLogDao;
	@Resource(name = "gsrtBankCustSuperviseInfoManager")
    private GsrtBankCustSuperviseInfoManager gsrtBankCustSuperviseInfoManager;
	@Resource(name = "gsrtBankServiceInfoManager")
    private GsrtBankServiceInfoManager gsrtBankServiceInfoManager;
	
	
	@Override
	public GsrtBankSuperviseLog save(HfCommonHead head, GsrtBankSuperviseLog log) {
		if(null == log){
			return null;
		}
		//操作员
		log.setOperatorId(head.getOperatorid());
		//流水号
		log.setBankOrderId(head.getOrderid());
		return this.gsrtBankSuperviseLogDao.save(log);
	}

    @Override
    public GsrtBankSuperviseLog save(GsrtBankSuperviseLog gsrtBankSuperviseLog) {
        return gsrtBankSuperviseLogDao.save(gsrtBankSuperviseLog);
    }

	@Override
	public void update(GsrtBankSuperviseLog gsrtBankSuperviseLog) {
		gsrtBankSuperviseLogDao.update(gsrtBankSuperviseLog);
	}

	@Override
	public GsrtBankSuperviseLog saveBankSuperviseLog(String merId,String serviceId,
			String logType,String reqMsg) {
		//获取商户的托管行信息
		List<GsrtBankCustSuperviseInfo> queryList = this.gsrtBankCustSuperviseInfoManager.
			getBankCustSuperviseInfo(merId);
		if(!queryList.isEmpty()){
			//查询服务表
	    	GsrtBankServiceInfo queryService = new GsrtBankServiceInfo();
	    	queryService.setBankSuperviseId(queryList.get(0).getBankSuperviseId());
	    	queryService.setBankServiceId(serviceId);
	    	GsrtBankServiceInfo serviceInfo = this.gsrtBankServiceInfoManager.findServiceInfoById(queryService);
	    	
	    	GsrtBankSuperviseLog log = new GsrtBankSuperviseLog();
	        log.setBankSuperviseId(queryList.get(0).getBankSuperviseId());
	        log.setMerCustId(merId);
	        // TODO 三个订单状态咋保存
	        log.setBankOrderId("-");
	        log.setGopayOrderId("-");
	        log.setMerOrderId("-");
	        log.setBankType(logType);
	        log.setCreatedTs(DateUtils.getSystemDate());
	        log.setLastUpdTs(DateUtils.getSystemDate());
	        log.setBankServiceId(serviceInfo.getBankServiceId());
	        log.setBankState(BANKSTATE.UNSEND.value);
	        //获取不到
	        log.setOperatorId("-");
	        log.setPlatId(queryList.get(0).getPlatId());
	        //获取不到
	        log.setBidNo("-");
	        log.setBidState("-");
	        log.setReqMsg(reqMsg);
	        return this.gsrtBankSuperviseLogDao.save(log);
		}
		return null;
	}
	

	/**
	 * 通过发标信息表数据组织日志
	 * @param gsrtCustTenderMaster
	 * @return
	 */
	@Override
	public GsrtBankSuperviseLog getGsrtBankSuperviseLog(GsrtCustTenderMaster gsrtCustTenderMaster) {
		GsrtBankSuperviseLog gsrtBankSuperviseLog = new GsrtBankSuperviseLog();

		Date curDate = DateUtils.getSystemDate();
		gsrtBankSuperviseLog.setMerOrderId(gsrtCustTenderMaster.getMerOrderId()); // 商户订单号
		gsrtBankSuperviseLog.setGopayOrderId(gsrtCustTenderMaster.getGopayOrderId()); // 国付宝订单号
		gsrtBankSuperviseLog.setMerCustId(gsrtCustTenderMaster.getMerCustId()); // 商户ID
		gsrtBankSuperviseLog.setBankServiceId("213123123"); // TODO: 2016/5/5   托管银行平台ID
		gsrtBankSuperviseLog.setBankSuperviseId(Long.parseLong("213123123")); // TODO: 2016/5/5 G商融通银行服务id
		gsrtBankSuperviseLog.setBidState(gsrtCustTenderMaster.getBankState()); //  标的状态
		gsrtBankSuperviseLog.setBankType(HfSuperviseCode.BANKTYPE.ORG_TO_BANK.value); // 服务类型  01 机构到银行 ；02 银行到机构
		gsrtBankSuperviseLog.setBidNo(gsrtCustTenderMaster.getBidNo()); // 标号
		gsrtBankSuperviseLog.setCreatedTs(curDate);
		gsrtBankSuperviseLog.setBankState(HfSuperviseCode.BANKSTATE.SENDING.value); //发送银行状态 默认为发送中 04
		gsrtBankSuperviseLog.setLastUpdTs(curDate);
		gsrtBankSuperviseLog.setPlatId(""); // TODO: 2016/5/5 银行平台ID 在哪取

		return gsrtBankSuperviseLog;
	}

	@Override
	public List<GsrtBankSuperviseLog> findGsrtBankSuperviseLogById(long logId) {
		Map<String,Object> param = new HashMap<String,Object>();
		StringBuffer hql = new StringBuffer("from GsrtBankSuperviseLog log where 1=1 ");
		if(0 != logId){
			hql.append(" and log.id=:id");
			param.put("id", logId);
		}
		return this.gsrtBankSuperviseLogDao.findByHql(hql.toString(), param);
	}
}
